package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeChange;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.service.IVeApplyService;
import org.edu.modules.sxjy.service.IVeChangeService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeChangeAssess;
import org.edu.modules.sxjy.vo.VeChangeInfoVo;
import org.edu.modules.sxjy.vo.VeChangeSubmitVo;
import org.edu.modules.sxjy.vo.VeChangeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"实习申请变更"})
@RestController
@RequestMapping({"/sxjy/veChange"})
@Component
@ApiSort(60)
public class VeChangeController
        extends BaseController<VeChange, IVeChangeService>
{
    private static final Logger log = LoggerFactory.getLogger(VeChangeController.class);
    @Autowired
    private IVeChangeService veChangeService;
    @Autowired
    private IVePracticeService vePracticeService;
    @Autowired
    private IVeApplyService veApplyService;

    @AutoLog("实习申请变更-查询列表")
    @ApiOperation(value="查询列表", notes="实习申请变更-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeChangeVo veChangeVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeChangeVo> veChangeVoList = this.veChangeService.selectAll(veChangeVo, startLine, pageSize, veCommon);
        int total = this.veChangeService.getSumPage(veChangeVo, veCommon);
        if ((Objects.isNull(veChangeVoList)) && (veChangeVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veChangeVoList));
    }

    @AutoLog("根据userId和userType查询是否可申请变更")
    @ApiOperation(value="根据userId和userType查询是否可申请变更", notes="根据userId和userType查询是否可申请变更")
    @GetMapping({"/getCheckChange"})
    public Result<?> getCheckChange(VeCommon veCommon)
    {
        if (!veCommon.getUserType().contains("2")) {
            return Result.ok(Boolean.valueOf(false));
        }
        VeChange veChange = this.veChangeService.getChangeByUserIdAndStatus(veCommon.getUserId(), Integer.valueOf(0));
        if (veChange != null) {
            return Result.ok(Boolean.valueOf(false));
        }
        return Result.ok(Boolean.valueOf(true));
    }

    @AutoLog("实习申请变更-通过id删除")
    @ApiOperation(value="通过id删除", notes="实习申请变更-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        return this.veChangeService.deleteById(id);
    }

    @AutoLog("实习申请变更-查看实习变更申请")
    @ApiOperation(value="查看实习变更申请", notes="实习申请变更-查看实习变更申请")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id)
    {
        VeChangeInfoVo veChangeInfoVo = this.veChangeService.getOneInfo(id);
        if (veChangeInfoVo == null) {
            return Result.OK();
        }
        return Result.OK(veChangeInfoVo);
    }

    @AutoLog("实习申请变更-查看评定")
    @ApiOperation(value="查看评定", notes="实习申请变更-查看评定")
    @GetMapping({"/selectAssess"})
    public Result<?> selectAssess(int id)
    {
        VeChangeAssess veChangeAssess = this.veChangeService.selectAssess(id);
        if (veChangeAssess == null) {
            return Result.OK();
        }
        return Result.OK(veChangeAssess);
    }

    @AutoLog("实习申请变更-新增信息")
    @ApiOperation(value="新增信息", notes="实习申请变更-新增信息")
    @PostMapping({"/add"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> add(@RequestBody VeChangeSubmitVo veChangeSubmitVo)
    {
        return this.veChangeService.saves(veChangeSubmitVo);
    }

    @AutoLog("实习申请变更-获取学生原实习单位和岗位信息")
    @ApiOperation(value="获取学生原实习单位和岗位信息", notes="实习申请变更-获取学生原实习单位和岗位信息")
    @GetMapping({"/getStuOldCpAndPost"})
    public Result<?> getStuOldCpAndPost(@RequestParam(name="userId") String userId)
    {
        return this.veChangeService.getStuOldCpAndPost(userId);
    }

    @AutoLog("实习申请变更-编辑回显")
    @ApiOperation(value="编辑回显", notes="实习申请变更-编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VeChangeSubmitVo veChangeSubmitVo = this.veChangeService.editEcho(id);
            return Result.OK(veChangeSubmitVo);
        }
        return Result.error("编辑失败");
    }

    @AutoLog("实习申请变更-编辑")
    @ApiOperation(value="编辑", notes="实习申请变更-编辑")
    @PutMapping({"/edit"})
    public Result<?> edit(@RequestBody VeChangeSubmitVo veChangeSubmitVo)
    {
        return this.veChangeService.edit(veChangeSubmitVo);
    }

    @AutoLog("实习申请变更-批量同意通过")
    @ApiOperation(value="批量同意通过", notes="实习申请变更-批量同意通过")
    @PutMapping({"/updatePass"})
    public Result<?> updatePass(@RequestBody List<VeChange> veChangeList)
    {
        if (veChangeList.size() > 0)
        {
            this.veChangeService.updatePass(veChangeList);
            return Result.OK("批量同意成功");
        }
        return Result.error("批量同意失败");
    }

    @AutoLog("实习申请变更-批量不同意通过")
    @ApiOperation(value="批量不同意通过", notes="实习申请变更-批量不同意通过")
    @PutMapping({"/updateNoPass"})
    public Result<?> updateNoPass(@RequestBody List<VeChange> veChangeList)
    {
        if (veChangeList.size() > 0)
        {
            this.veChangeService.updateNoPass(veChangeList);
            return Result.OK("批量不同意成功");
        }
        return Result.error("批量不同意失败");
    }

    @AutoLog("实习申请变更-查看学生当前的状态是否可申请实习")
    @ApiOperation(value="查看学生当前的状态是否可申请实习", notes="实习申请变更-查看学生当前的状态是否可申请实习")
    @GetMapping({"/stuIsStatusChange"})
    public Result<?> stuIsStatusChange(String userType, String userId)
    {
        return this.veChangeService.stuIsStatusChange(userType, userId);
    }

    @AutoLog("实习申请变更-查看学生是否是分散实习是否可申请实习变更")
    @ApiOperation(value="查看学生是否是分散实习是否可申请实习变更", notes="实习申请变更-查看学生是否是分散实习是否可申请实习变更")
    @GetMapping({"/stuDisperseIsChange"})
    public Result<?> stuDisperseIsChange(String userType, String userId)
    {
        return this.veChangeService.stuDisperseIsChange(userType, userId);
    }
}
